<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;
use App\Models\Issue;
use Illuminate\Support\Facades\Log;




class CommentController extends Controller
{
    /**
     * @OA\Post(
     *     path="/api/issues/{issueId}/comments",
     *     summary="添加评论",
     *     tags={"Issue"},
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="issueId",
     *         in="path",
     *         required=true,
     *         description="ID of the issue to add a comment to",
     *         @OA\Schema(type="integer", example=1)
     *     ),
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             required={"content"},
     *             @OA\Property(property="content", type="string", example="This is a new comment")
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="Comment added successfully",
     *         @OA\JsonContent(
     *             @OA\Property(property="message", type="string", example="OK"),
     *             @OA\Property(
     *                 property="comment",
     *                 ref="#/components/schemas/Comment"
     *             )
     *         )
     *     ),
     *     @OA\Response(response="401", description="未授权"),
     *     @OA\Response(response="404", description="issue不存在"),
     * )
     */

    public function addComment(Request $request, $issueId)
    {
        $request->validate(['content' => 'required|string']);

        $user = auth()->user();
        // printf("user=%s\n", json_encode($user));

        $recIssue = Issue::find($issueId);
        if (!$recIssue) {
            LOG::error(sprintf("issue not found|userName=%s|issueId=%d\n", $user->name, $issueId));
            return response()->json(['message' => 'Issue not found',], 404);
        }

        $comment = Comment::create([
            'content' => $request->content,
            'user_id' => $user->id,
            'issue_id' => $issueId
        ]);

        LOG::debug(sprintf("addComment|userName=%s|issueId=%d|commentId=%d\n", $user->name, $issueId, $comment->id));


        return response()->json(['message' => 'OK', 'comment' => $comment], 200);
    }


    /**
     * @OA\Post(
     *     path="/api/issues/{issueId}/transfer",
     *     summary="转移 issue 给其他用户",
     *     tags={"Issue"},
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="issueId",
     *         in="path",
     *         required=true,
     *         description="ID of the issue to transfer",
     *         @OA\Schema(type="integer", example=1)
     *     ),
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             required={"des_user_id"},
     *             @OA\Property(property="des_user_id", type="integer", example=2)
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="Issue transferred successfully",
     *         @OA\JsonContent(
     *             @OA\Property(property="message", type="string", example="issue transfer ok")
     *         )
     *     ),
     *     @OA\Response(
     *         response="400",
     *         description="Bad request",
     *         @OA\JsonContent(
     *             @OA\Property(property="message", type="string", example="User not found"),
     *             @OA\Property(property="issueOwner", type="integer", example=1),
     *             @OA\Property(property="currUserId", type="integer", example=2)
     *         )
     *     ),
     *     @OA\Response(response="401", description="未鉴权"),
     *     @OA\Response(response="404", description="issue不存在"),
     *     @OA\Response(response="403", description="issue 权限问题，owner不正确"),
     * )
     */
    public function transferComment(Request $request, $issueId)
    {
        $request->validate(['des_user_id' => 'required|int']);

        $user = auth()->user();
        // printf("user=%s\n", json_encode($user));

        $resIssue = Issue::where('id', $issueId)->first();
        if (!$resIssue) {
            return response()->json(['message' => 'issue not found'], 404);
        }

        // printf("resIssue=%s|des_user_id=%d\n", json_encode($resIssue), $request->des_user_id);
        LOG::debug(sprintf("transferComment, param|userName=%s|issueId=%d|des_user_id=%d", $user->name, $issueId, $request->des_user_id));

        if ($resIssue->user_id != $user->id) {
            LOG::error(sprintf("error, not issue owner|issueOwner=%d|currUserId=%d", $resIssue->user_id, $user->id));

            return response()->json([
                'message' => 'not issue owner',
                'issueOwner' => $resIssue->user_id,
                'currUserId' => $user->id,
            ], 403);
        }

        $resIssue->update(['user_id' => $request->des_user_id]);

        LOG::debug(sprintf("transferComment OK|userName=%s|issueId=%d|des_user_id=%d", $user->name, $issueId, $request->des_user_id));

        return response()->json(['message' => 'OK'], 200);
    }
}
